/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import dao.AttributeHome;
import dao.DeletedMobilesHome;
import dao.MobileHasAttributeHome;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import dao.MobileHome;
import dao.UserHasMobileHome;
import dao.UserHome;
import java.util.ArrayList;
import pojo.Mobile;
import pojo.User;
import pojo.UserHasMobileId;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author mustafajnr
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory = null;
    private static final MobileHome mobileDAO;
    private static final UserHome userDAO;
    private static final UserHasMobileHome hasMobileDAO;
    private static AttributeHome attributeDAO;
    private static MobileHasAttributeHome hasAttributeDAO;
    private static DeletedMobilesHome deletedMobilesDAO;

    static {
        if (sessionFactory == null) {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        mobileDAO = new MobileHome(sessionFactory);
        userDAO = new UserHome(sessionFactory);
        hasMobileDAO = new UserHasMobileHome(sessionFactory);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static MobileHome getMobileDAO() {
        return mobileDAO;
    }

    public static UserHasMobileHome getHasMobileDAO() {
        return hasMobileDAO;
    }

    public static UserHome getUserDAO() {
        return userDAO;
    }

    public static AttributeHome getAttributeDAO() {
        return attributeDAO;
    }

    public static MobileHasAttributeHome getHasAttributeDAO() {
        return hasAttributeDAO;
    }

    public static DeletedMobilesHome getDeletedMobilesDAO() {
        return deletedMobilesDAO;
    }
    
    public static int addToCart(int userId, int mobileId) {
        pojo.Mobile mobile = getMobileDAO().getMobileById(mobileId);
        if (mobile.getQuantity() == 0) {
            return 1;
        }
        pojo.User user = getUserDAO().getUserById(userId);
        if (mobile.getPrice() > user.getCreditBalance()) {
            return 2;
        }

        getHasMobileDAO().addUserHasMobile(new pojo.UserHasMobile(user, mobile, false));
        mobile.setQuantity(mobile.getQuantity() - 1);
        getMobileDAO().updateMobile(mobile);
        user.setCreditBalance(user.getCreditBalance() - mobile.getPrice());
        getUserDAO().updateUser(user);
        return 0;
    }

    public static void checkOut(ArrayList<UserHasMobileId> checkedOutPhones) {
        getHasMobileDAO().checkOutPhones(checkedOutPhones);
    }

    public static void toggleActivation(int userId) {
        User user = getUserDAO().getUserById(userId);
        user.setActivated(!user.isActivated());
        getUserDAO().updateUser(user);
    }

    public static void addMobile(Mobile mobile) {
        getMobileDAO().createMobile(mobile);
    }

}
